Shopify Webhooks
publishには、GraphQLかAdminAPI で登録するしか方法はない。管理画面上にはない(はず)
注意点:
5秒以内に200を返すことに気をつける
5秒以内に200 を返す。Google Apps Scriptの doPostで受けてると、時に厳しいよう
2023/09, 結論は pub/sub経由でやるのが良い。
code: receive_webhook.js
function doPost(e){
let p = JSON.parse(e.postData.getDataAsString());
myProc(p);
}
function myProc(p){
const id = p.admin_graphql_api_id;
Logger.log(id);
if(id.search(/Order/)>-1){
procOrder(p);
}else if(id.search(/Customer/)>-1) {
if("user") procRegister(p);
}else{
console.error("invalid webhook");
}
}
function procRegister(p){
const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("users");
const users = [p.id, p.email, p.first_name, p.last_name, p.state, p.note, p.verified_emkail, p.phone, p.addresses0]; sh.appendRow(users);
}
function procOrder(p){
const orders = p.line_items.map(x => {
const ret = {}
ret.fk = x.sku;
ret.quantity = x.quantity;
return ret
});
const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("orders")
Logger.log(orders);
orders.forEach(row => {
sh.appendRow([
p.created_at,
p.customer.email,
p.customer.id,
p.name,
p.id,
p.processing_method,
row.fk,
row.quantity,
${p.billing_address.last_name} ${p.billing_address.first_name},
${p.billing_address.province}${p.billing_address.city}${p.billing_address.address1}${p.billing_address.address2},
p.billing_address.phone,
p.billing_address.zip,
${p.shipping_address.last_name} ${p.shipping_address.first_name},
p.shipping_address.phone,
p.shipping_address.zip,
${p.shipping_address.province}${p.shipping_address.city}${p.shipping_address.address1}${p.shipping_address.address1}
])
});
}